<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses ( 'Controller', 'Controller' );
App::uses ( 'AuthComponent', 'Controller/Component' );
App::import('Vendor', 'Smarty', array('file' => 'smarty'.DS.'Smarty.class.php')); // for use pure smarty
/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
	public $viewClass = 'Smarty';
	
	//config Acl, Auth compponent
	 public $components = array (
			'Acl',
			'Auth' => array (
					'authenticate' => array (
							'Form' => array (
									'userModel' => 'User',
									'fields' => array (
											'username' => 'username',
											'password' => 'password' 
									) 
							) 
					),
					
					'authorize' => array (
							'Actions' => array (
									'actionPath' => 'controllers' 
							) 
					),
					'loginAction' => array (
							'controller' => 'users',
							'action' => 'login' 
					),
					'authError' => 'You don\'t have access here.' 
			),
			'Session' 
	); 
	
	/* public $components = array (
			'Acl',
			'Auth' => array (
					'authenticate' => array (
							'Form' => array (
									'userModel' => 'User',
									'fields' => array (
											'username' => 'username',
											'password' => 'password'
									)
							)
					),
						
					'authorize' => 'Controller', 
					'loginAction' => array (
							'controller' => 'users',
							'action' => 'login'
					),
					'authError' => 'You don\'t have access here.'
			),
			'Session'
	); */
	
	public $helpers = array (
			 'SmartyHtml',
			/*'SmartyView.SmartyForm',
			'SmartyView.SmartySession',
			'SmartyView.SmartyJavascript',	 */		
			'Html',
			'Session'
	);
	public function beforeFilter() {
		// Configure AuthComponent
		 $this->Auth->loginAction = array (
				'controller' => 'users',
				'action' => 'login' 
		);
		$this->Auth->logoutRedirect = array (
				'controller' => 'posts',
				'action' => 'index' 
		);
		$this->Auth->loginRedirect = array (
				'controller' => 'posts',
				'action' => 'index' 
		);
		 
		// check if user already logined and the controller is adminposts
		// and check if in the specific controller, need authorize
		 if (/* $this->Auth->user () && */ $this->params ['controller'] == 'adminposts') {
			
		 	//if the group of current user doesn't have permission for specific controllerthen kick it out
			if (! $this->Acl->check ( $this->Auth->user ( 'role' ), $this->params ['controller'] )) {
				$this->Session->setFlash ( 'Your account is not authorized to use this function', 'default', array (
						'class' => 'alert alert-warning' 
				) );
				return $this->redirect($this->Auth->redirect());
			} 
		} 
	}
	
	
/* 	public function isAuthorized($user = null) {
		echo 'in isAuthorize';
		var_dump($this->Acl->check ( $this->Auth->user ( 'role' ), $this->params ['controller'] )); */
				
	/* 	if (! $this->Acl->check ( $this->Auth->user ( 'role' ), $this->params ['controller'] )) {
			$this->Session->setFlash ( 'You are not authorized to use this function', 'default', array (
					'class' => 'alert alert-warning'
			) );
		
			$this->redirect ( array (
					'controller' => 'users',
					'action' => 'abcxvcxvxc'
			) );
		}else{
			$this->Auth->allow ( 'index', 'detail', 'listByMonth', 'checkConfig');
		} */
		
	/* 	if($this->Auth->user()){
			$this->redirect ( array (
					'controller' => 'users',
					'action' => 'abcxvcxvxc'
			) );
		} */
		
	/* 	echo 'in authorized method';
		var_dump('<pre>', $this->Auth->user());
		exit;
		// Any registered user can access public functions
		if (empty($this->request->params['admin'])) {
			return true;
		}
	
		// Only admins can access admin functions
		if (isset($this->request->params['admin'])) {
			return (bool)($user['role'] === 'admin');
		}
	
		// Default deny
		return false; */
// 	}
}
